﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for Order
/// </summary>
public class Order
{

    SqlCommand cmd;
    SqlDataReader dr;
	public Order()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public List<ObjOrder> getOrderByStatus(string sql)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_OrderGetByStatus", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[1];
        sqla[0] = new SqlParameter("sql", DbType.String);
        sqla[0].Value = sql;
        cmd.Parameters.AddRange(sqla);
        dr = cmd.ExecuteReader();
        ObjOrder objOrder;
        List<ObjOrder> lst = new List<ObjOrder>();
        while (dr.Read())
        {
            objOrder = new ObjOrder();
            objOrder.Order_id = int.Parse(dr["order_id"].ToString());
            objOrder.Ho_ten = dr["ho_ten"].ToString();
            objOrder.Tinh = dr["tinh"].ToString();
            objOrder.Dia_chi = dr["dia_chi"].ToString();
            objOrder.So_dien_thoai = dr["so_dien_thoai"].ToString();
            objOrder.Ngay_tao = dr["ngay_tao"].ToString();
            objOrder.Status = int.Parse(dr["status"].ToString());
            objOrder.Email = dr["email"].ToString();
            lst.Add(objOrder);
        }
        sqlConnection.Close();
        return lst;
        
    }

    public List<ObjOrder> getOrderSearch(string sql)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_OrderTimKiem", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[1];
        sqla[0] = new SqlParameter("txt", DbType.String);
        sqla[0].Value = sql;
        cmd.Parameters.AddRange(sqla);
        dr = cmd.ExecuteReader();
        ObjOrder objOrder;
        List<ObjOrder> lst = new List<ObjOrder>();
        while (dr.Read())
        {
            objOrder = new ObjOrder();
            objOrder.Order_id = int.Parse(dr["order_id"].ToString());
            objOrder.Ho_ten = dr["ho_ten"].ToString();
            objOrder.Tinh = dr["tinh"].ToString();
            objOrder.Dia_chi = dr["dia_chi"].ToString();
            objOrder.So_dien_thoai = dr["so_dien_thoai"].ToString();
            objOrder.Ngay_tao = dr["ngay_tao"].ToString();
            objOrder.Status = int.Parse(dr["status"].ToString());
            objOrder.Email = dr["email"].ToString();
            lst.Add(objOrder);
        }
        sqlConnection.Close();
        return lst;

    }
    public void createOrder(string xml)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_OrderContactOrderInsert", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[1];
        sqla[0] = new SqlParameter("doc", DbType.String);
        sqla[0].Value = xml;
        cmd.Parameters.AddRange(sqla);
        cmd.ExecuteNonQuery();
        sqlConnection.Close();
    }

    //update giao hang
    public void updateOrderGiaoHang(int status, int order_id)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_OrderUpdateStatus", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[2];
        sqla[0] = new SqlParameter("status", DbType.Int16);
        sqla[1] = new SqlParameter("order_id", DbType.Int32);
        sqla[0].Value = status;
        sqla[1].Value = order_id;
        cmd.Parameters.AddRange(sqla);
        cmd.ExecuteNonQuery();
        sqlConnection.Close();


    }
    //delete order by xml
    public void deleteOrderByXml(string xml)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_OrderDeleteByXml", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[1];
        sqla[0] = new SqlParameter("doc", DbType.String);
        
        sqla[0].Value = xml;
        
        cmd.Parameters.AddRange(sqla);
        cmd.ExecuteNonQuery();
        sqlConnection.Close();


    }
}